"use strict";

/**
 * @ngdoc service
 * @name jgappApp.MapService
 * @description
 * # MapService
 * Service in the jgappApp.
 */
angular.module("jgappApp").service("MapService", function($q, Util, $http) {
  // AngularJS will instantiate a singleton by calling "new" on this function
  var MapService = {};
  MapService.createInstance = function(opts, element) {
    // create map instance
    var map = new BMap.Map(element[0]);
    // init map, set central location and zoom level
    map.centerAndZoom(
      new BMap.Point(opts.center.longitude, opts.center.latitude),
      opts.zoom
    );
    if (opts.navCtrl) {
      // add navigation control
      map.addControl(new BMap.NavigationControl());
    }
    if (opts.scaleCtrl) {
      // add scale control
      map.addControl(new BMap.ScaleControl());
    }
    if (opts.overviewCtrl) {
      //add overview map control
      map.addControl(new BMap.OverviewMapControl());
    }
    if (opts.enableScrollWheelZoom) {
      //enable scroll wheel zoom
      map.enableScrollWheelZoom();
    }

    // set the city name
    // map.setCurrentCity(opts.city);
    return map;
  };

  var _createMarker = function(marker, pt) {
    var markerinstance;
    // if (marker.icon) {
    //     var icon = new BMap.Icon(marker.icon, new BMap.Size(marker.width, marker.height));
    //     markerinstance = BMap.Marker(pt, { icon: icon });
    //     markerinstance.enableDragging();
    //     return markerinstance
    // }
    markerinstance = new BMap.Marker(pt);
    return markerinstance;
  };
  MapService.redrawMarkers = function(map, previousMarkers, opts) {
    // previousMarkers.forEach(function({ marker, listener }) {
    //     marker.removeEventListener('click', listener);
    //     map.removeOverlay(marker);
    // });
    // previousMarkers.length = 0;
    map.clearOverlays(previousMarkers);
    if (!opts.markers) {
      return;
    }
    opts.markers.forEach(function(marker) {
      var marker2 = _createMarker(
        marker,
        new BMap.Point(marker.longitude, marker.latitude)
      );
      map.addOverlay(marker2);
      if (opts.enableDragging) {
        marker2.enableDragging();
      }
      var previousMarker = { marker: marker2, listener: null };
      previousMarkers.push(previousMarker);
      if (!marker.title && !marker.content) {
          return;
      }
      var msg = "<p>"+marker.title+"</p><p>"+marker.content+"</p>";
      var infoWindow2 = new BMap.InfoWindow(msg, {
          enableMessage: !!marker.enableMessage
      });
      previousMarker.listener = function() {
          this.openInfoWindow(infoWindow2);
      };
      marker2.addEventListener('click', previousMarker.listener);
    });
  };

  MapService.setAllMarker = function(map, myValue, opts, previousMarkers) {
    map.clearOverlays(); //清除地图上所有覆盖物
    var local = new BMap.LocalSearch(map, {
      //智能搜索
      onSearchComplete: function() {
        var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
        map.centerAndZoom(pp, opts.zoom);
        var marker = new BMap.Marker(pp);
        map.addOverlay(marker); //添加标注
        if (opts.enableDragging) {
          marker.enableDragging();
        }
        var curMasker = { marker: marker, listener: null };
        previousMarkers.push(curMasker);
      }
    });
    local.search(myValue);
  };
  MapService.setMarker = function(map, opts, point,previousMarkers) {
    map.clearOverlays(); //清除地图上所有覆盖物
    map.centerAndZoom(point, opts.zoom);
    var marker = new BMap.Marker(point);
    map.addOverlay(marker); //添加标注
    if (opts.enableDragging) {
      marker.enableDragging();
    }
    var curMasker = { marker: marker, listener: null };
    previousMarkers.push(curMasker);
  };
  // 根据经纬度获取当前地址
  MapService.getLocation = function(point) {
    var defer = $q.defer();
    var point = new BMap.Point(point.longitude, point.latitude);
    var geoc = new BMap.Geocoder();
    geoc.getLocation(point, function(rs) {
      var addComp = rs.address;
      defer.resolve(addComp);
    });
    return defer.promise;
  };
  MapService.getcurPositon = function() {
    var positiontip = "获取位置信息失败";
    var defer = $q.defer();
    // android定位与ios定位方法不同
    NativeUtil.use("getLatLng", function(positon) {
      if (positon) {
        defer.resolve(JSON.parse(positon));
      } else {
        Util.Toast(positiontip);
        defer.reject(positiontip);
      }
    });
    return defer.promise;
  };
  MapService.getDistance = function(p1, p2) {
    var param = {};
    param.p1 = p1;
    param.p2 = p2;
    var distance = NativeUtil.use("getDistance", null, param);
    return distance;
  };
  // 计算是否允许打卡
  MapService.permissionDistance = function(p1, p2, deviation) {
    var distance;
    distance = MapService.getDistance(p1, p2);
    return distance < deviation;
  };
  MapService.getAk = function() {
    var ak;
    if (Util.getPlatform() == "android") {
      ak = "LvGLjO3Sb832PyNx2EfzovtglbSIR0Mm";
    } else {
      ak = "8W5P6KRFBF6NXs8GbsEnjPfcjcHS4n1b";
    }
    return ak;
  };
  return MapService;
});
